
define(['vue',
		'config',
		'common',
		'../model/upLoadImg',
],function(Vue,config,utils,upLoadImg){
	'use strict';
	
	var shopApplicationView = new Vue({
		el:'#app',
		data:{
			province:'',
			city:'',
			district:'',
			licenseNumber:'',

			onprogress:''
		},
		created:function(){
			utils.userToken();
		  	this.applyshops = utils.getData('applyshops');
		},
		methods:{
			//第一次上传
			selectPhoto:function(event){

				var $target = $(event.currentTarget);
				var $targetPic = $(event.currentTarget).parents('.Again').find('.imgAgain');

				$targetPic.show();
				$target.parents('.report-file').hide();

				var pic = upLoadImg.showPic($targetPic,$target);
				/*var _this = this;
				_this.onprogress = function(evt){
					var loaded = evt.loaded;     //已经上传大小情况 
					var tot = evt.total;      	 //附件总大小 
					var per = Math.floor(100*loaded/tot);  //已经上传的百分比
				    $targetPic.find('.upLoadimg').css({'height':(100-per)+'%'});
				    if(per === 100){
				    	_this.onprogress = '';
				    }
				}
				upLoadImg.ajax(upLoadImg.uploadFile($target),_this.onprogress,$targetPic);*/
			},
			//再次上传
			selectAgain:function(event){
				var $target = $(event.currentTarget);
				var $targetPic = $target.parent('.imgAgain');
				var pic = upLoadImg.showPic($targetPic,$target);
				$target.parents('.Again').find('.file-prew').addClass('remove');

				/*var _this = this;
				_this.onprogressAgain = function(evt){
					$targetPic.find('.upLoadimg').css({'height':100+'%'})
					var loaded = evt.loaded;     //已经上传大小情况 
					var tot = evt.total;      //附件总大小 
					var per = Math.floor(100*loaded/tot);  //已经上传的百分比
				    $targetPic.find('.upLoadimg').css({'height':(100-per)+'%'})
				    if(per === 100){
				    	_this.onprogressAgain = '';
				    }
				}
				upLoadImg.ajax(upLoadImg.uploadFile($target),_this.onprogressAgain,$targetPic);*/
			},

			toBankAccount:function(){
				var _this = this;
				
				var getPic = function(element,isLoading){
					var pic;
					if(isLoading === "t"){
						pic = $(element).find('.pic').attr('src');
					}else{
						pic = $(element).find('.pic');
					}
					return pic;
				}

				var getFiles = function(element){
					var files;
					var element = $(element);
					if(element.hasClass('remove')){
						return files = element.parents('.Again').find('.inputAgain').get(0).files[0];
					}else{
						return files = element.get(0).files[0];
					}
				}

				if($.trim(_this.licenseNumber) === ""){
					utils.appAlert('企业营业执照号不能为空');
                    return;
				}

				if(_this.licenseNumber.length < 15){
					utils.appAlert('企业营业执照号不能少于15位');
                    return;
				}

				if($.trim(getPic('#businessLicensePic','t')) === ""){
					utils.appAlert('请上传营业执照照片');
                    return;
				}
				
				if($.trim(getPic('#legalPic1Pic','t')) === ""){
					utils.appAlert('请上传法人身份证正面照片');
                    return;
				}

				if($.trim(getPic('#legalPic2Pic','t')) === ""){
					utils.appAlert('请上传法人身份证反面照片');
                    return;
				}

				if($.trim(getPic('#holdPicPic','t')) === ""){
					utils.appAlert('请上传负责人手持身份证照片');
                    return;
				}

				if($.trim(getPic('#facadePhotoPic','t')) === ""){
					utils.appAlert('请上传店面门头照片');
                    return;
				}

				var filesList = {
					businessLicense: getFiles('#businessLicense'),
					legalPic1: getFiles('#legalPic1'),
					legalPic2: getFiles('#legalPic2'),
					holdPic: getFiles('#holdPic'),
					facadePhoto: getFiles('#facadePhoto'),
					proxyPic: getFiles('#proxyPic')
				}

				utils.appAlert('当前上传图片数量较大,请在wifi环境下进行操作',null,
					'取消',function(){
						var onprogress = function(evt){
							var loaded = evt.loaded;     //已经上传大小情况 
							var tot = evt.total;      	 //附件总大小 
							var per = Math.floor(100*loaded/tot);  //已经上传的百分比
						    $('.upLoadimg').css({'height':(100-per)+'%'});
						}

						var format = upLoadImg.uploadFile(filesList);
						upLoadImg.ajax(format,onprogress,function(res){
							var urls = res.urls;
							getPic('#businessLicensePic').attr('src',urls[0]);
							getPic('#legalPic1Pic').attr('src',urls[1]);
							getPic('#legalPic2Pic').attr('src',urls[2]);
							getPic('#holdPicPic').attr('src',urls[3]);
							getPic('#facadePhotoPic').attr('src',urls[4]);
							getPic('#proxyPicPic').attr('src',urls[5]);

							var applyshops = utils.getData('applyshops');
							var data = {
								shopsName: applyshops.shopsName,
								shopsAddress: applyshops.shopsAddress,
								shopsLinkman: applyshops.shopsLinkman,
								shopsLinkphone: applyshops.shopsLinkphone,
								licenseNumber: _this.licenseNumber,
								businessLicense: urls[0],
								legalPic1: urls[1],
								legalPic2: urls[2],
								holdPic: urls[3],
								facadePhoto: urls[4],
								proxyPic: urls[5],
								province: applyshops.provinceID,
								city: applyshops.cityID,
								county: applyshops.countyID,
								area: applyshops.areaID
							}

							utils.ajax(utils.getWebUrl('shop/apply'),data,'POST',function(res){
								window.location.href="shopApplication_verify.html";
							})
						})
					},'继续操作','提示')

				
			}
		}
	})

	return shopApplicationView
});